package com.club.common.security;


import com.club.common.constant.Constant;
import com.club.common.exception.CaptchaException;
import com.club.util.RedisUtil;
import com.club.util.StringUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 验证码过滤器
 *
 * @author chaochao(vx : zhangfec)
 */

@RequiredArgsConstructor
@Component
@Slf4j
public class CaptchaFilter extends OncePerRequestFilter {

    private final RedisUtil redisUtil;

    private final LoginFailureHandler loginFailureHandler;

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        String url = httpServletRequest.getRequestURI();

//        if ("/login".equals(url) && httpServletRequest.getMethod().equals("POST")) {
//            try {
//                // 校验验证码
//                validate(httpServletRequest);
//            } catch (CaptchaException e) {
//
//                // 交给认证失败处理器
//                loginFailureHandler.onAuthenticationFailure(httpServletRequest, httpServletResponse, e);
//            }
//        }

        filterChain.doFilter(httpServletRequest, httpServletResponse);
    }

    // 校验验证码逻辑
    private void validate(HttpServletRequest httpServletRequest) {

        String code = httpServletRequest.getParameter("code");
        String key = httpServletRequest.getParameter("uuid");

        if (StringUtil.isEmpty(code) || StringUtil.isEmpty(key)) {
            throw new CaptchaException("验证码错误");
        }

        if (!code.equals(redisUtil.hget(Constant.CAPTCHA_KEY, key))) {
            throw new CaptchaException("验证码错误");
        }

    }
}
